package com.yy.wga.business.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.mp.common.dto.UserCacheDTO;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.admin.request.AdminBusinessCourseVideoPageRequest;
import com.yy.wga.mp.module.admin.response.AdminBusinessCourseVideoPageResponse;
import com.yy.wga.mp.module.business.entity.pojo.BusinessCourseVideoDO;
import com.yy.wga.mp.module.business.mapper.BusinessCourseVideoMapper;
import com.yy.wga.mp.module.business.response.BusinessCourseVideoInfoResponse;
import com.yy.wga.mp.module.business.service.BusinessCourseVideoService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商学院课程视频(BusinessCourseVideo)表服务实现类
 *
 * @author guochao
 * @since 2025-02-09 15:18:38
 */
@Service
public class BusinessCourseVideoServiceImpl extends ServiceImpl<BusinessCourseVideoMapper, BusinessCourseVideoDO> implements BusinessCourseVideoService {
    @Override
    public PageBean<AdminBusinessCourseVideoPageResponse> pageData(AdminBusinessCourseVideoPageRequest request) {
        LambdaQueryWrapper<BusinessCourseVideoDO> queryWrapper = new LambdaQueryWrapper<BusinessCourseVideoDO>()
                .eq(BusinessCourseVideoDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(request.getCourseId() != null, BusinessCourseVideoDO::getCourseId, request.getCourseId());
        if (request.getStatus() != null) {
            // 状态  0：未开始 1：已上架 2：已下架
            if (request.getStatus() == 0) {
                queryWrapper.ne(BusinessCourseVideoDO::getStatus, 2)
                        .ge(BusinessCourseVideoDO::getStartTime, new Date());
            } else if (request.getStatus() == 1) {
                Date now = new Date();
                queryWrapper.ne(BusinessCourseVideoDO::getStatus, 2)
                        .le(BusinessCourseVideoDO::getStartTime, now)
                        .ge(BusinessCourseVideoDO::getEndTime, now);
            } else {
                // status = 2 or endTime < now
                queryWrapper.and(wrapper -> wrapper.eq(BusinessCourseVideoDO::getStatus, 2)
                        .or(query -> query.lt(BusinessCourseVideoDO::getEndTime, new Date())));
            }
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<BusinessCourseVideoDO> configs = baseMapper.selectList(queryWrapper);
        Date now = new Date();
        for (BusinessCourseVideoDO config : configs) {
            if (config.getStatus() == 2) {
                continue;
            }
            if (now.before(config.getStartTime())) {
                config.setStatus(0);
            } else if (now.after(config.getEndTime())) {
                config.setStatus(2);
            } else {
                config.setStatus(1);
            }
        }
        PageBean<AdminBusinessCourseVideoPageResponse> pageBean = new PageBean<>();
        pageBean.setPageInfo(configs);
        pageBean.setList(BeansUtils.mapList(configs, AdminBusinessCourseVideoPageResponse.class));
        return pageBean;
    }

    @Override
    public List<BusinessCourseVideoInfoResponse> list(List<Long> courseList) {
        Date now = new Date();
        List<BusinessCourseVideoDO> resourceConfigList = baseMapper.selectList(new LambdaQueryWrapper<BusinessCourseVideoDO>()
                .eq(BusinessCourseVideoDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .lt(BusinessCourseVideoDO::getStartTime, now)
                .gt(BusinessCourseVideoDO::getEndTime, now)
                .in(BusinessCourseVideoDO::getStatus, Arrays.asList(0, 1))
                .eq(BusinessCourseVideoDO::getCourseId, courseList));
        List<BusinessCourseVideoInfoResponse> responseList = new ArrayList<>(resourceConfigList.size());
        UserCacheDTO userCache = RequestUserHolder.getUserCache();
        for (BusinessCourseVideoDO courseVideo : resourceConfigList) {
            BusinessCourseVideoInfoResponse response = BeansUtils.map(resourceConfigList, BusinessCourseVideoInfoResponse.class);
            if (userCache.getGrade() > courseVideo.getUnlockMinGrade()) {
                response.setUnlocked(Boolean.TRUE);
            } else if (userCache.getGrade().equals(courseVideo.getUnlockMinGrade())
                    && userCache.getLevel() >= courseVideo.getUnlockMinLevel()) {
                response.setUnlocked(Boolean.TRUE);
            }
            responseList.add(response);
        }
        return responseList;
    }
}
